package com.sys.controller;

import com.sys.common.aop.Operation;
import com.sys.common.constant.Constant;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import com.sys.pojo.Permission;
import com.sys.service.PermissionService;
import com.sys.service.RoleService;
import com.sys.vo.RolePermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Api(tags = {"菜单权限管理"})
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Resource
    private RoleService roleService;


    @GetMapping("/getMenu")
    @RequiresPermissions("menu:list")
    @ApiOperation("获取当前用户菜单")
    public ResponseDateUtil getMenu(@RequestParam("uId") Integer uId) {
        return ResultBuilder.success(permissionService.getPermission(uId));
    }

    @PostMapping("/listAllPermission")
    @RequiresPermissions("permission:list")
    @ApiOperation("获取全部权限包括菜单、目录、按钮")
    public ResponseDateUtil getAllPermission(@RequestBody Permission permission) {
        return ResultBuilder.success(permissionService.findList(permission));
    }

    @PostMapping("/listAllMenu")
    @ApiOperation("获取全部的菜单、目录")
    public ResponseDateUtil getAllMenu() {
        return ResultBuilder.success(permissionService.findMenuList());
    }

    @PostMapping("/addPermission")
    @RequiresPermissions("permission:add")
    @ApiOperation("添加权限")
    @Operation("添加权限")
    public ResponseDateUtil addPermission(@RequestBody Permission permission) {
        if (permissionService.add(permission) > 0) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }


    @PostMapping("/relationPermission")
    @RequiresPermissions("permission:relation")
    @ApiOperation("角色关联权限")
    @Operation("角色关联权限")
    public ResponseDateUtil relationPermission(@RequestBody RolePermissionVo vo) {
        if (roleService.addRolePermission(vo)) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }


    @PutMapping("/updPermission")
    @RequiresPermissions("permission:upd")
    @ApiOperation("更新权限信息")
    @Operation("更新权限信息")
    public ResponseDateUtil updPermission(@RequestBody Permission permission) {
        if (permissionService.updateData(permission) > 0) {
            return ResultBuilder.success(Constant.UPDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.UPDFAILED);
        }
    }

    @DeleteMapping("/delPermission")
    @RequiresPermissions("permission:del")
    @ApiOperation("删除权限")
    @Operation("删除权限")
    public ResponseDateUtil delPermission(@RequestParam("id") Integer id) {
        if (permissionService.delete(id) > 0) {
            return ResultBuilder.success(Constant.DELSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.DELFAILED);
        }
    }


    @GetMapping("/getRolePermission")
    @ApiOperation("获取角色权限")
    public ResponseDateUtil getRolePermission(@RequestParam("rId") Integer rId) {
        return ResultBuilder.success(permissionService.getRolePermission(rId));
    }


}
